package com.kaiyidiannao.activity.foodpalace;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.kaiyidiannao.adapter.CommonViewpagerAdapter;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.PlayBean;
import com.kaiyidiannao.model.req.model.GetFoodPalaceShow;
import com.kaiyidiannao.model.req.model.PostAddFavorites;
import com.kaiyidiannao.view.ActionBarView;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 14-4-2.
 * 玩乐住详情
 */
public class PlayDetailActivity extends BaseActionBarActivity implements View.OnClickListener, Observer {
    //view相关
    private TextView product_name; //商品名称
    private ImageView collect; //商品收藏按钮
    private TextView product_detail; //商品详细介绍
    private TextView telephone;//电话
    private TextView areaid; //所在区域
    private TextView address; //地址
    private LinearLayout item1, item2, item3, play_hourse;
    private TextView service_name1, service_name2, service_name3, service_price1, service_price2, service_price3;
    private View line1, line2;
    private String userid;
    private PlayBean bean;
    private static final String[] fanweis = {"范围1000m以内", "范围2000m以内", "范围3000m以内", "范围4000m以内", "范围5000m以内"};
    private String typeid = "49";
    private ImageView youHuiImage;
    private boolean collectFlag;
    // 玩乐住详情的catid是固定的
    private String catid = DataCenter.CATID_PLAY + "";
    private String itemid;
    private TextView houseJieShao;
    //用来区分 娱乐 休闲 住宿
    private String catids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.play_detail_activity);
        DataCenter.getInstance().addObserver(this);

        findViews();
        //处理ActionBar
        findActionBar();

        showProgressDialog();
        findViewById(R.id.play_detail_scrollview).setVisibility(View.INVISIBLE);
        findViewById(R.id.realod_layout).setVisibility(View.GONE);
        houseJieShao = (TextView) findViewById(R.id.houseJieShao);
        catids = getIntent().getStringExtra("catids");
        Log.d("dasdasdasdasdas", catids);

        itemid = getIntent().getStringExtra("itemid");
        if (isLogin()) {
            userid = getUserid();
            new GetFoodPalaceShow(Integer.parseInt(itemid), userid, catid).execute();
        } else {
            new GetFoodPalaceShow(Integer.parseInt(itemid), catid).execute();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DataCenter.getInstance().deleteObserver(this);
    }

    private void findViews() {
        product_name = (TextView) findViewById(R.id.paly_product_name);
        collect = (ImageView) findViewById(R.id.collect);
        product_detail = (TextView) findViewById(R.id.play_detail_text);
        address = (TextView) findViewById(R.id.play_address);
        telephone = (TextView) findViewById(R.id.play_telphone);
        areaid = (TextView) findViewById(R.id.play_areaid);
        // youHuiImage = (ImageView) findViewById(R.id.youHuiImage);
        //八张详情图片
        image_details = (ImageView) findViewById(R.id.details_image);
        detailViewPager = (ViewPager) findViewById(R.id.detail_ViewPager);
        item1 = (LinearLayout) findViewById(R.id.item1);
        item2 = (LinearLayout) findViewById(R.id.item2);
        item3 = (LinearLayout) findViewById(R.id.item3);
        service_name1 = (TextView) findViewById(R.id.service_name1);
        service_name2 = (TextView) findViewById(R.id.service_name2);
        service_name3 = (TextView) findViewById(R.id.service_name3);
        service_price1 = (TextView) findViewById(R.id.service_price1);
        service_price2 = (TextView) findViewById(R.id.service_price2);
        service_price3 = (TextView) findViewById(R.id.service_price3);
        play_hourse = (LinearLayout) findViewById(R.id.play_hourse);
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
    }

    /**
     * 初始化8张详情图片
     */
    private ImageView image_details;//商品图标
    private ViewPager detailViewPager;
    private CommonViewpagerAdapter commonViewpagerAdapter;
    private List<View> imageList = new ArrayList<View>();
    private List<String> imageUrlList = new ArrayList<String>();

    private void initViewPage() {
        imageUrlList = bean.getPicture2(); //只有传bean的需要
        commonViewpagerAdapter = new CommonViewpagerAdapter(PlayDetailActivity.this, imageList, detailViewPager, imageUrlList);

        for (int i = 0; i < imageUrlList.size(); i++) {
            final int position = i;
            View view = getLayoutInflater().inflate(R.layout.common_viewpager_item, null);
            final ImageView viewpagerItemIv = (ImageView) view.findViewById(R.id.viewpagerItemIv);
            viewpagerItemIv.setImageResource(android.R.color.white);
            imageLoader.displayImage(imageUrlList.get(i), viewpagerItemIv);
            viewpagerItemIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Intent intent = new Intent();
//                    intent.setClass(PlayDetailActivity.this, DisplayActivity.class);
//                    intent.putExtra("url", imageUrlList.get(position));
//                    startActivity(intent);  Intent intent = new Intent();
//                    intent.setClass(PlayDetailActivity.this, DisplayActivity.class);
//                    intent.putExtra("url", imageUrlList.get(position));
//                    startActivity(intent);
                    //上面的方法废弃了
                    PlayDetailActivity.super.startImagePagerActivity(position, bean.getPicture());
                }
            });
            imageList.add(view);
        }

        detailViewPager.setAdapter(commonViewpagerAdapter);
    }


    private void initViews() {
        product_name.setText("" + bean.getTitle());
        collect.setOnClickListener(this);
        product_detail.setText(bean.getIntroduce());
        address.setText(bean.getAddress());
        telephone.setText(bean.getTelephone());
        areaid.setText(bean.getAreaid());

        if (bean.isIs_fav()) {// 已经收藏
            collect.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.shoucanged));
            // collect.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.xingxing));
            collectFlag = true;
        } else if (!bean.isIs_fav()) {
            collect.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.shoucang));
            // collect.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.favoritesed));
        }

        //八张详情图片
        imageUrlList = bean.getPicture2();
        if (imageUrlList.size() == 0) {
            image_details.setVisibility(View.VISIBLE);
            detailViewPager.setVisibility(View.INVISIBLE);
        } else {
            detailViewPager.setVisibility(View.VISIBLE);
            image_details.setVisibility(View.INVISIBLE);
        }
        initViewPage();
    }

    /**
     * 显示重新加载页面
     */
    private void showReloadView() {
        findViewById(R.id.play_detail_scrollview).setVisibility(View.GONE);
        findViewById(R.id.realod_layout).setVisibility(View.VISIBLE);
        findViewById(R.id.reload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                findViewById(R.id.play_detail_scrollview).setVisibility(View.INVISIBLE);
                findViewById(R.id.realod_layout).setVisibility(View.GONE);
                showProgressDialog();
                if (isLogin()) {
                    userid = getUserid();
                    new GetFoodPalaceShow(Integer.parseInt(itemid), userid, catid).execute();
                } else {
                    new GetFoodPalaceShow(Integer.parseInt(itemid), catid).execute();
                }
            }
        });
    }

    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.setTitle("玩乐住详情");
    }

    @Override
    public void onClick(View v) {
        if (!HttpUtils.isConnected()) {
            Toast.makeText(PlayDetailActivity.this, "网络连接异常,请稍后重试", Toast.LENGTH_SHORT).show();
            return;
        }
        if (null != bean) {
            if (isLogin()) {
                switch (v.getId()) {
                    case R.id.collect:
                        String action = "add";
                        if (collectFlag) {
                            action = "delete";
                        }
                        //玩乐住规定catid=49
                        showProgressDialog();
                        new PostAddFavorites(DataCenter.CATID_PLAY, userid, action, bean.getItemid(), bean.getTitle()).execute();
                        collect.setEnabled(false);
                }
            } else {
                startLoginOrRegistered();
            }
        } else {
            showToast("详情加载失败");
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;

        if (flag == DataCenter.flag.GetAddMESSAGEINFO_SUCCESS) {//收藏成功,关注成功\
            if (DataCenter.code.equals(DataCenter.successFlag)) {
                if (collectFlag) {//从当前的关注状态变成没有关注
                    collectFlag = false;
                    showToast(R.string.collect_cancle);
                    collect.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.shoucang));
                } else {
                    showToast(R.string.collect_add);
                    collectFlag = true;
                    collect.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.shoucanged));
                }
                if (isLogin()) {
                    userid = getUserid();
                    new GetFoodPalaceShow(Integer.parseInt(itemid), userid, catid).execute();
                } else {
                    new GetFoodPalaceShow(Integer.parseInt(itemid), catid).execute();
                }
            } else {
                showToast(DataCenter.msg, "收藏出现错误");
            }
        } else {
            closeProgressDialog();
        }


        if (flag == DataCenter.flag.GET_PLAY_SHOW_SUCCESS) {
            closeProgressDialog();
            collect.setEnabled(true);
            ArrayList<PlayBean> list = DataCenter.foodPalaceList;
            bean = list.get(0);
            if (catids.equals("52")) {
                houseJieShao.setText("房间介绍");
                if (!TextUtils.isEmpty(bean.getN1())) {
                    item1.setVisibility(View.VISIBLE);
                    service_name1.setText(bean.getN1());
                    service_price1.setText(bean.getV1());
                }
                if (!TextUtils.isEmpty(bean.getN2())) {
                    line1.setVisibility(View.VISIBLE);
                    item2.setVisibility(View.VISIBLE);
                    service_name2.setText(bean.getN2());
                    service_price2.setText(bean.getV2());
                }
                if (!TextUtils.isEmpty(bean.getN3())) {
                    line2.setVisibility(View.VISIBLE);
                    item3.setVisibility(View.VISIBLE);
                    service_name3.setText(bean.getN3());
                    service_price3.setText(bean.getV3());
                }
                if (TextUtils.isEmpty(bean.getN1()) && TextUtils.isEmpty(bean.getV1()) && TextUtils.isEmpty(bean.getN2()) && TextUtils.isEmpty(bean.getV2()) && TextUtils.isEmpty(bean.getN3()) && TextUtils.isEmpty(bean.getV3())) {
                    play_hourse.setVisibility(View.GONE);
                }
            } else if (catids.equals("50")) {
                houseJieShao.setText("娱乐介绍");
                if (!TextUtils.isEmpty(bean.getItem())) {
                    item1.setVisibility(View.VISIBLE);
                    service_name1.setText(bean.getItem());
                    service_price1.setText(bean.getPrice());
                } else {
                    play_hourse.setVisibility(View.GONE);
                }
            } else if (catids.equals("51")) {
                houseJieShao.setText("休闲介绍");
                if (!TextUtils.isEmpty(bean.getItem())) {
                    item1.setVisibility(View.VISIBLE);
                    service_name1.setText(bean.getItem());
                    service_price1.setText(bean.getPrice());
                } else {
                    play_hourse.setVisibility(View.GONE);
                }
            }
            if (bean != null) {
                if (bean.getPicture().length == 0) {
                    detailViewPager.setBackgroundResource(R.drawable.ic_default);
                }
            }
            imageUrlList = bean.getPicture2();
            Log.d("GetYhpic", bean.getYhpic());
//            if (TextUtils.isEmpty(bean.getYhpic())) {
//                youHuiImage.setVisibility(View.GONE);
//            } else {
//                imageLoader.displayImage(bean.getYhpic(),youHuiImage,options,null);
//            }
            findViewById(R.id.play_detail_scrollview).setVisibility(View.VISIBLE);
            initViews();
        } else if (flag == DataCenter.flag.GET_PLAY_SHOW_FAILED) {
            closeProgressDialog();
            showReloadView();
        }

    }
}